<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/16 0016
 * Time: 上午 10:44
 */
namespace app\admin\model;

use think\Db;
use think\Model;

class Order extends Model{

    public function manager(){
        return $this->belongsTo('Manager','mid','id');
    }

    public function user(){
        return $this->belongsTo('User','uid','id');
    }

    public function orderlist(){
        return $this->hasMany('OrderList','oid');
    }

    /**id获取订单详情
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function orderdetail($id){
        $order = self::with('orderlist')->find($id);
        return $order->toArray();
    }

    /**获取所有订单（废弃）
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getall(){
        $order = self::with('orderlist')->where(['is_deleted'=>1])->order('id desc')->select();
        return $order->toArray();
    }

    /**根据uid和状态获取订单（前端用户订单）
     * @param $uid
     * @param $status
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getallByuid($uid,$status){
        $order = self::with('orderlist')->where(['is_deleted'=>1,'uid'=>$uid])->order('id desc');
        if (!empty($status)){
            $order->where('status',$status);
        }
        return $order->select();
    }

    /**关闭超时未支付订单
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function closeorder($uid){
        $order = self::with('orderlist')->where(['uid'=>$uid,'status'=>1,'is_deleted'=>1])->where('out_time','<',date('Y-m-d H:i:s'));
        $data = [];
        foreach ($order->select() as $k=>$v){
            foreach ($v['orderlist'] as $key=>$vo){
                $data[] = ['id'=>$vo['mgl_id'],'num'=>$vo['num']];
            }
        }
        ManagerGoodsList::reStock($data);
        $order->update(['is_deleted'=>2]);
    }

    /**根据用户，结算订单/商品的积分返利
     * @param $uid
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public static function jiesuan($uid){
        $order = self::with('orderlist')
            ->where(['uid'=>$uid])
            ->where('jiesuan_time','<',date('Y-m-d H:i:s'))
            ->where('status','in',[4,5])->select();
        foreach ($order as $k=>$v){
            if ($v['status']==4 && $v['is_jiesuan']==0){
                //该订单状态为4，且未结算
                if (conf('jifentwo_r')*floor($v['pay']/conf('jifentwo_l'))>0){
                    Db::name('Jifen')->insert([
                        'type'=>2,
                        'uid'=>$uid,
                        'jifen'=>conf('jifentwo_r')*floor($v['pay']/conf('jifentwo_l')),
                        'create_at'=>$v['jiesuan_time']
                    ]);
                }
                Db::name('Order')->where('id',$v['id'])->update(['is_jiesuan'=>1]);
            }elseif ($v['status']==5 && $v['shouhou_status']!=3){
                //有售后的订单，需要检查商品状态
                foreach ($v['orderlist'] as $key=>$vo){
                    //商品不是售后且未结算 || 商品售后不通过且未结算
                    if (($vo['status']==1 && $vo['is_jiesuan']==0) || ($vo['status']==5 && $vo['is_jiesuan']==0)){
                        if (conf('jifentwo_r')*floor($vo['pay']/conf('jifentwo_l'))>0){
                            Db::name('Jifen')->insert([
                                'type'=>2,
                                'uid'=>$uid,
                                'jifen'=>conf('jifentwo_r')*floor($vo['pay']/conf('jifentwo_l')),
                                'create_at'=>$v['jiesuan_time']
                            ]);
                        }

                        Db::name('OrderList')->where('id',$vo['id'])->update(['is_jiesuan'=>1]);
                    }
                }
            }
        }

    }
}